package com.seaboxdata.dao;

import com.seaboxdata.util.DataSourceConnectionFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * @author
 * @create 2021-05-27 16:59
 **/
@Component
@Slf4j
public class BatchInterfaceDao {

    private Connection conn;

    public void createTable(String sql) throws SQLException {
        if(conn == null){
            conn = DataSourceConnectionFactory.getDbConnection();
        }
        conn.setAutoCommit(false);
        PreparedStatement preparedStatement = conn.prepareStatement(sql);
        preparedStatement.execute();
        conn.commit();
    }

    public int batchInsert(String sql) throws SQLException{
        if(conn == null){
            conn = DataSourceConnectionFactory.getDbConnection();
        }
        conn.setAutoCommit(false);
        PreparedStatement preparedStatement = conn.prepareStatement(sql);
        int count = preparedStatement.executeUpdate(sql);
        conn.commit();
        return count;
    }
}
